﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ProjectBot.Events;
using System.Drawing;

namespace ProjectBot.Managers
{
    public class GitHubManager
    {
        public delegate void UpdatedProject(object sender, GitHubChangedArgs e);
        public event UpdatedProject ProjectUpdate = null;
        private static Dictionary<string, GitHubProject> Projects = new Dictionary<string, GitHubProject>();
        private static RssFeedReader Reader = new RssFeedReader();
        /*
         * TODO
         * <?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xml:lang="en-US">
  <id>tag:github.com,2008:/Pactor/projectbot/commits/master</id>
  <link type="text/html" rel="alternate" href="https://github.com/Pactor/projectbot/commits/master"/>
  <link type="application/atom+xml" rel="self" href="https://github.com/Pactor/projectbot/commits/master.atom"/>
  <title>Recent Commits to projectbot:master</title>
  <updated>2013-03-04T23:04:20-08:00</updated>
  <entry>
    <id>tag:github.com,2008:Grit::Commit/aaa1083c1b86a416b4935946260a982db77716ff</id>
    <link type="text/html" rel="alternate" href="https://github.com/Pactor/projectbot/commit/aaa1083c1b86a416b4935946260a982db77716ff"/>
    <title>
        first commit
    </title>
    <updated>2013-03-04T23:04:20-08:00</updated>
    <media:thumbnail height="30" width="30" url="https://secure.gravatar.com/avatar/ae9d855eedbca3ff020786400bd49e7e?s=30&amp;d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png"/>
    <author>
      <name>Pactor</name>
      <uri>https://github.com/Pactor</uri>
    </author>
    <content type="html">
         * bunch of files modified
         * + Nebo/Contacts/ContactList.cs
+ Nebo/Contacts/ContactsIsOnTracker.cs
+ Nebo/Contacts/ContactsMonitorTracker.c
         * 
         */
        public static bool TryGetProject(string userName, string projectName, out GitHubProject project)
        {
            project = null;
            string link = ConvertUserAndProject(userName, projectName);
            project = new GitHubProject(userName, projectName);
            if (project.Doc.InnerXml == string.Empty) { return false; }
            else
                return true;
        }
        public GitHubProject TryGetProject(string gitPullLink)
        {
            GitHubProject project = null;
            string link = ConvertGitLink(gitPullLink);
            //project = new GitHubProject(Reader.Read(link));
            return project;
        }
        public static string ConvertUserAndProject(string userName, string projectName)
        {
            string reply = "https://github.com/";
            try
            {
                reply += userName + "/";
                reply += projectName + "/commits/master.atom";
            }
            catch { ProjectBot.ProjectBotForm.WriteLine("Error in parsing to Link", Color.Red); }
            return reply;
        }
        public static string ConvertGitLink(string link)
        {
            // https://github.com/Pactor/projectbot.git < what we are converting
            // we want https://github.com/Pactor/projectbot/commits/master.atom
            string reply = "https://github.com/";
            try
            {
                // first we parse the username out of it
                string userName = link.Substring(reply.Length).Split('/').First();
                reply += userName + "/";
                // now we want the projectname
                string projectName = link.Split('/').Last().Split('.').First();
                reply += projectName + "/commits/master.atom";
            }
            catch { ProjectBot.ProjectBotForm.WriteLine("Error in parsing to Link", Color.Red); }
            return reply;
        }
        public static bool Addproject(string user, GitHubProject project)
        {
            try
            {
                //if(Projects[
            }
            catch { }
            return false;
        }
        public static bool RemoveProject(GitHubProject project)
        {
            //GitHubProject toRemove = null;
            //foreach (GitHubProject pro in Projects)
            //{
                //if(project.ProjectName
            //}
            return false;
        }
        public static List<GitHubProject> GetAllProjects()
        {
            List<GitHubProject> newProjects = new List<GitHubProject>();
            try
            {
            }
            catch { }
            return newProjects;
        }
        private string SearchForNextUpdate(XmlDocument doc)
        {
            return doc.InnerXml;
        }
    }
}
